package com.lbagroup.giscovery;

import org.json.JSONObject;

import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
import com.lbagroup.giscovery.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/** 
 * Activity for loading Facebook login dialog
 * 
 * This activity is used to call the facebook login dialog in order to verify the user 
 * 
 * @author XH 
 * @version 2011.1107 
 * @since 0.2 
 */  
public class FBLogin extends Activity {
   
	/** 
	 * The Facebook object that connects the application to an Facebook ID. 
	 */ 
	Facebook facebook = new Facebook("185337608187267");

	/** 
	 * The preference file that saves the settings in local phone disk 
	 */ 
    public static final String MO_PROF="ProferenceFile"; 
    
    /** 
     * The variable that saves the facebook user id in local storage 
     */ 
    public static final String FB_USERID = "FBUserid";
    
    /** 
     * The variable that saves the facebook user full name in local storage 
     */ 
    public static final String FB_USERFULLNAME = "FBUserfullname";
    
    /** 
     * The variable that saves the facebook username in local storage 
     */ 
    public static final String FB_USERNAME = "FBUsername";

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fb_login_screen);
   
        /** 
         * This step triggers the facebook authorization dialog 
         */ 
        facebook.authorize(FBLogin.this,new String[] { "read_stream" },Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
           
            @Override
            public void onComplete(Bundle values) {
            	try{
            		String strProfile=facebook.request("me"); // after the authorization completes, access the current user
            		
            		try {
            			// the following code access the current user's user full name, user id, and username in Facebook
            			// and save these values to local drive through the preference file. 
            			JSONObject json = Util.parseJson(strProfile);
            			getSharedPreferences(MO_PROF,MODE_PRIVATE)
            	        .edit()
            	        .putString(FB_USERFULLNAME, json.getString("name"))
            	        .putString(FB_USERID, json.getString("id"))
            	        .putString(FB_USERNAME, json.getString("username"))
              	        .commit();
            			finish();
        			} catch (FacebookError e) {

        			}
            	}
            	catch (Exception e) {
            		
            	}
            	finish();
            }

            @Override
            public void onFacebookError(FacebookError error) {}

            @Override
            public void onError(DialogError e) {}

            @Override
            public void onCancel() {}
        });
   
    }
    
    /** 
     * The call-back method which is triggered after the authorization is completed.  
     * 
     */ 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        facebook.authorizeCallback(requestCode, resultCode, data);
    }
    
}